﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Common;
using GtsServer.DAL;
using GtsServer.Model;
using System.Data;

namespace GtsServer.Manage.Controller
{
    public class UserController
    {
        UserDAL dal = new UserDAL();
        public List<UserShow> GetList(string imsi,string sysAccount, string userAccount, int pageIndex, int pageSize, out int count)
        {
            count = 0;
            List<UserShow> list = new List<UserShow>();
            string strWhere = " where 1=1 ";
            if (!string.IsNullOrEmpty(imsi))
            {
                strWhere += " and u.imsi like '" + imsi + "%' ";
            }
            if (!string.IsNullOrEmpty(sysAccount))
            {
                strWhere += " and u.userAccount='" + sysAccount + "' ";
            }
            if (!string.IsNullOrEmpty(userAccount))
            {
                strWhere += " and ui.name='" + userAccount + "' ";
            }
            DataSet ds = dal.GetList(pageIndex, pageSize, strWhere);
            count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            list = IListDataSet.DataSetToIList<UserShow>(ds, 0).ToList();
            return list;
        }

        public List<UserShow> GetRobotList()
        {
            List<UserShow> list = new List<UserShow>();
            DataSet ds = dal.GetList(" IsRobot =1");
            list = IListDataSet.DataSetToIList<UserShow>(ds, 0).ToList();
            return list;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userInfoID"></param>
        /// <param name="userName"></param>
        /// <returns>1:添加成功 0：用户名已存在</returns>
        public int EditUserName(int userInfoID, string userName)
        {
            DataSet ds = dal.EditUserName(userInfoID, userName);
            int state = 0;
            if (ds != null && ds.Tables[0].Rows.Count > 0)
                state = Convert.ToInt32(ds.Tables[0].Rows[0]["state"].ToString());

            return state;
        }

        public int Add(User models)
        {
            return dal.Add(models);
        }
        public bool Update(User model)
        {
            return dal.Update(model);
        }
        public bool Delete(int ID)
        {
            return dal.Delete(ID);
        }
    }
}
